﻿// <copyright file="FeedService.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace Channel9Kinect.Services
{
    #region

    using System;
    using System.ComponentModel.Composition;
    using System.IO;
    using System.Net;
    using System.ServiceModel.Syndication;
    using System.Threading.Tasks;
    using System.Xml;

    using MediaRss;

    #endregion

    [Export]
    public class FeedService
    {
        #region Constants and Fields

        public static readonly Uri C4f = new Uri("http://channel9.msdn.com/coding4fun/tv/RSS");

        public static readonly Uri Ch9 = new Uri("http://channel9.msdn.com/Shows/This+Week+On+Channel+9/RSS");

        public static readonly Uri Ping = new Uri("http://channel9.msdn.com/Shows/PingShow/RSS");

        public static readonly Uri SLTV = new Uri("http://channel9.msdn.com/Shows/SilverlightTV/RSS");

        public static readonly Uri Webcamp = new Uri("http://channel9.msdn.com/Shows/Web+Camps+TV/RSS");

        #endregion

        #region Constructors and Destructors

        public FeedService()
        {
        }

        #endregion

        #region Methods

        public async Task<SyndicationFeed> GetFeeds(Uri uri)
        {
            var webClient = new WebClient();
            var content = await webClient.DownloadStringTaskAsync(uri);
            var parseFeeds = await TaskEx.Run(() => this.ParseFeeds(content));
            return parseFeeds;
        }

        private SyndicationFeed ParseFeeds(string content)
        {
            return SyndicationFeed.Load<MediaRssFeed>(XmlReader.Create(new StringReader(content)));
        }

        #endregion
    }
}